package com.yaozheng.vocationaltraining.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.widget.ListView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.adapter.LearnListContentAdapter;
import com.yaozheng.vocationaltraining.adapter.LearnListTypeAdapter;
import com.yaozheng.vocationaltraining.adapter.event.ItemClickListener;
import com.yaozheng.vocationaltraining.iview.ICategoriesView;
import com.yaozheng.vocationaltraining.service.CategoriesService;
import com.yaozheng.vocationaltraining.service.impl.CategoriesServiceImpl;
import com.yaozheng.vocationaltraining.utils.JsonUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.LearnListTypeItemView;

/**
 * 学习列表(二级目录)
 *
 * @author Administrator
 */
@EFragment(R.layout.fragment_learn_list)
public class LearnListFragment extends BaseFragment implements ICategoriesView, ItemClickListener<LearnListTypeItemView> {
    @ViewById
    ListView typeListView, contentListView;

    @ViewById
    TextView headTitleText;
    /**
     * 类型设备器
     */
    LearnListTypeAdapter learnListTypeAdapter;
    /**
     * 内容设备器
     */
    LearnListContentAdapter learnListContentAdapter;
    //	Map<String, List<JSONObject>> listContentMap;
    JSONArray listContentMap;
    /**
     * 默认选中
     */
    int selectLearnIndex;
    @Bean(CategoriesServiceImpl.class)
    CategoriesService categoriesService;

    LearnListTypeItemView learnListTypeItemView;


    @AfterViews
    public void initView() {
        categoriesService.init(this);

        headTitleText.setText(getBaseActivity().getIntentString("learnListTitle"));
        selectLearnIndex = getBaseActivity().getIntentInt("selectLearnIndex", 0);
           /*
        learnListTypeAdapter = new LearnListTypeAdapter(
                (BaseActivity) getActivity(), this);

//		listContentMap = new HashMap<String, List<JSONObject>>();
        listContentMap = new JSONArray();
        JSONArray content1 = new JSONArray();
//        List<JSONObject> content1 = new ArrayList<JSONObject>();
        content1.add(JsonUtils.getJsonObject("{\"id\":5,\"name\":\"宝沃由来\",\"imageResourcesId\":" + R.drawable.brand_subclass_img1 + "}"));
        content1.add(JsonUtils.getJsonObject("{\"id\":6,\"name\":\"宝沃归来\",\"imageResourcesId\":" + R.drawable.brand_subclass_img2 + "}"));
        content1.add(JsonUtils.getJsonObject("{\"id\":7,\"name\":\"宝沃未来\",\"imageResourcesId\":" + R.drawable.brand_subclass_img2 + "}"));

        JSONArray content2 = new JSONArray();
        content2.add(null);
        content2.add(JsonUtils.getJsonObject("{\"id\":8,\"name\":\"BX7\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));

//        content2.add(JsonUtils.getJsonObject("{\"id\":10,\"name\":\"销售话术\",\"imageResourcesId\":" + R.drawable.goods_subclass_img3 + "}"));

//        JSONArray content3 = new JSONArray();
//        content3.add(null);
//        content3.add(null);
//        content3.add(JsonUtils.getJsonObject("{\"id\":9,\"name\":\"技术基础\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));


//        content3.add(JsonUtils.getJsonObject("{\"id\":10,\"name\":\"岗位认证\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));
        JSONArray content3 = new JSONArray();
        content3.add(null);
        content3.add(null);
        content3.add(JsonUtils.getJsonObject("{\"id\":11,\"name\":\"销售基础\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));

        JSONArray content4 = new JSONArray();
        content4.add(null);
        content4.add(null);
        content4.add(null);
        content4.add(JsonUtils.getJsonObject("{\"id\":12,\"name\":\"服务基础\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));


        JSONArray content5 = new JSONArray();
        content5.add(null);
        content5.add(null);
        content5.add(null);
        content5.add(null);
        content5.add(JsonUtils.getJsonObject("{\"id\":13,\"name\":\"技术基础\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));
        JSONArray content6 = new JSONArray();

        content6.add(null);
        content6.add(null);
        content6.add(null);
        content6.add(null);
        content6.add(null);
        content6.add(JsonUtils.getJsonObject("{\"id\":14,\"name\":\"岗位认证\",\"imageResourcesId\":" + R.drawable.goods_subclass_img1 + "}"));


        JSONObject itemJsonObjectContent1 = new JSONObject();
        itemJsonObjectContent1.put("name", getString(R.string.learnBrandsStr));
        itemJsonObjectContent1.put("value", content1);
        itemJsonObjectContent1.put("imageResourcesId", R.drawable.icon_study_class1);
        itemJsonObjectContent1.put("isEnable", true);

        JSONObject itemJsonObjectContent2 = new JSONObject();
        itemJsonObjectContent2.put("name", getString(R.string.learnCommodityStr));
        itemJsonObjectContent2.put("value", content2);
        itemJsonObjectContent2.put("imageResourcesId", R.drawable.icon_study_class2);
        itemJsonObjectContent2.put("isEnable", true);

        JSONObject itemJsonObjectContent3 = new JSONObject();
        itemJsonObjectContent3.put("name", getString(R.string.learnSalesStr));
        itemJsonObjectContent3.put("value", content3);
        itemJsonObjectContent3.put("imageResourcesId", R.drawable.icon_study_class3);
        itemJsonObjectContent3.put("isEnable", true);
        JSONObject itemJsonObjectContent4 = new JSONObject();

        itemJsonObjectContent4.put("name", getString(R.string.learnServiceStr));
        itemJsonObjectContent4.put("value", content4);
        itemJsonObjectContent4.put("imageResourcesId", R.drawable.icon_study_class4);
        itemJsonObjectContent4.put("isEnable", true);

        JSONObject itemJsonObjectContent5 = new JSONObject();
        itemJsonObjectContent5.put("name", getString(R.string.learnTechnologyStr));
        itemJsonObjectContent5.put("value", content5);
        itemJsonObjectContent5.put("imageResourcesId", R.drawable.icon_study_class5);
        itemJsonObjectContent5.put("isEnable", true);

        JSONObject itemJsonObjectContent6 = new JSONObject();
        itemJsonObjectContent6.put("name", getString(R.string.learnPostCertificationStr));
        itemJsonObjectContent6.put("value", content6);
        itemJsonObjectContent6.put("imageResourcesId", R.drawable.icon_study_class6);
        itemJsonObjectContent6.put("isEnable", true);


        listContentMap.add(itemJsonObjectContent1);
        listContentMap.add(itemJsonObjectContent2);
        listContentMap.add(itemJsonObjectContent3);
        listContentMap.add(itemJsonObjectContent4);
        listContentMap.add(itemJsonObjectContent5);
        listContentMap.add(itemJsonObjectContent6);

        // 品牌
        // 宝沃由来/宝沃归来/宝沃未来
        //
        // 商品
        // BX7 USP/竞品对比/销售话术
        //
        // 销售
        // DOS流程/销售技巧/销售礼仪
        //
        // 服务
        // DOS流程/服务技巧/服务礼仪
        learnListContentAdapter = new LearnListContentAdapter(
                (BaseActivity) getActivity());
        contentListView.setAdapter(learnListContentAdapter);

        learnListTypeAdapter.setDataList(null);
        learnListTypeAdapter.setSelectPosition(selectLearnIndex);
        typeListView.setAdapter(learnListTypeAdapter);
*/
        categoriesService.categoriesList(0);


    }

    /**
     * 设置内容
     */
    public void setLearnListContent(JSONArray content, String key) {
        if (content != null) {
            headTitleText.setText(key);
            getBaseActivity().setIntentString("selectKey", key);
            learnListContentAdapter.setDataList(content);
            learnListContentAdapter.notifyDataSetChanged();
        }
    }

    @UiThread
    @Override
    public void categoriesListSuccess(JSONObject jsonObject, int id) {

        JSONArray courseCategories = TypeUtils.getJsonArray(jsonObject, "data", "course_categories");
        if (id == 0) {

            if (learnListTypeAdapter == null) {
                learnListTypeAdapter = new LearnListTypeAdapter(getBaseActivity(), this);
                learnListTypeAdapter.setDataList(courseCategories);
                typeListView.setAdapter(learnListTypeAdapter);
            } else {
                learnListTypeAdapter.setDataList(courseCategories);
                learnListTypeAdapter.notifyDataSetChanged();
            }
            // 显示子内容
            learnListTypeAdapter.setSelectPosition(selectLearnIndex);
            JSONObject itemData = TypeUtils.getJsonObject(courseCategories, selectLearnIndex);
            categoriesService.categoriesList(TypeUtils.getJsonInteger(itemData, "id"));
            //
        } else {
            cancelOperatingProgressDialog();
            if (learnListTypeItemView != null) {
                learnListTypeAdapter.setSelectPosition(learnListTypeItemView.getPosition());
                learnListTypeAdapter.notifyDataSetChanged();
            }
            for (int i = 0; i < learnListTypeAdapter.getSelectPosition(); i++) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("id", -1);
                courseCategories.add(0, jsonObject1);
            }
            if (learnListContentAdapter == null) {
                learnListContentAdapter = new LearnListContentAdapter(
                        (BaseActivity) getActivity());
                learnListContentAdapter.setDataList(courseCategories);
                contentListView.setAdapter(learnListContentAdapter);
            } else {
                learnListContentAdapter.setDataList(courseCategories);
                learnListContentAdapter.notifyDataSetChanged();
            }
            this.learnListTypeItemView = null;
        }
    }

    @UiThread
    @Override
    public void categoriesListError(String error, int id) {
        alertMessage(error);
        if (id == 0) {

        } else {
            cancelOperatingProgressDialog();
            learnListTypeItemView = null;
        }
    }


    @Override
    public void click(LearnListTypeItemView learnListTypeItemView) {
        if (this.learnListTypeItemView == null) {
            showOperatingProgressDialog("加载中.");
            this.learnListTypeItemView = learnListTypeItemView;
            JSONObject itemData = learnListTypeItemView.getItemData();
            categoriesService.categoriesList(TypeUtils.getJsonInteger(itemData, "id"));
        }
    }


    @Override
    public void onDestroyView() {
        learnListTypeAdapter = null;
        learnListTypeItemView = null;
        learnListContentAdapter = null;
        super.onDestroyView();
    }
}
